package com.xtsoft.platform.task.controller;


import com.fasterxml.jackson.databind.JsonMappingException;

import com.xtsoft.core.common.YesNo;
import com.xtsoft.core.controller.GenericController;
import com.xtsoft.core.repository.QueryFilter;
import com.xtsoft.core.repository.QueryResult;
import com.xtsoft.core.utils.ResultMsg;
import com.xtsoft.core.utils.StringUtils;

import com.xtsoft.platform.base.system.domain.SysUser;
import com.xtsoft.platform.globe.enums.TaskBugType;
import com.xtsoft.platform.globe.utils.SecurityUtils;
import com.xtsoft.platform.task.domain.TaskBug;
import com.xtsoft.platform.task.service.TaskBugService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParseException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.Date;

/**
 * 
 * @ClassName:  taskBug   
 * <p> 任务缺陷处理类 </p>
 * @author: Codegen-代码生成器
 * @date:   2019年12月3日 上午10:59:46
 */
 @Tag(name =  "任务缺陷--处理接口")
@RestController
public class TaskBugController extends GenericController{
	
	private final Logger log = LoggerFactory.getLogger(TaskBugController.class);
	
	@Autowired	
	private TaskBugService taskBugService;
	
	
    /**
     *  <p>新增一个任务缺陷缺陷</p>  
     * @param taskBug 要新增的任务缺陷
     * @return ResponseEntity<ResultMsg>, ResultMsg的Data中包含 任务缺陷
     * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常                     
	 * @author Codegen-代码生成器	
	 * @date 2020-07-16 12:10:57
    */	
    @PostMapping("/taskBug/add")
    @Parameter(name = "taskBug", description = "要新增的任务缺陷", required = false)
	@Operation(summary = "新增，根据前台传递的任务缺陷新增")
    public ResponseEntity<ResultMsg> add(@RequestBody TaskBug taskBug) throws URISyntaxException {
		if(null == taskBug){
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"新增任务缺陷失败，传递的任务缺陷为空"),HttpStatus.OK); 
		}SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("新增任务缺陷缺陷开始......",  user.getLoginId(), user.getUsername(), getClientIp());
        if (!StringUtils.isEmpty(taskBug.getTaskBugId())) {
		    log.info("新增任务缺陷缺陷失败，新任务缺陷不能具有taskBugId属性值",  user.getLoginId(), user.getUsername(), getClientIp());
            return new ResponseEntity<ResultMsg>(new ResultMsg(false,"新增任务缺陷失败，新任务缺陷不能具有taskBugId属性值"),HttpStatus.OK);
        }
        taskBug.setUserId(user.getUserId());
        taskBug.setState(YesNo.No.getIntValue());
        taskBug.setUpdateTime(new Date().getTime());
        TaskBug result = taskBugService.save(taskBug);
		log.info("新增任务缺陷成功",  user.getLoginId(), user.getUsername(), getClientIp());
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"新增成功",result),HttpStatus.OK);
    }	

    /**
     * <p>更新一个任务缺陷</p>    
     * @param taskBug 要更新的任务缺陷
     * @return ResponseEntity<ResultMsg>, json格式
     * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
     */
    @PostMapping("/taskBug/update")
    @Parameter(name = "taskBug", description = "要更新的任务缺陷", required = false)
	@Operation(summary = "更新，根据传递的 任务缺陷更新实体")
    public ResponseEntity<ResultMsg> update(@RequestBody TaskBug taskBug) throws URISyntaxException {
		if(null == taskBug){
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"更新任务缺陷失败，传递的任务缺陷为空"),HttpStatus.OK); 
		}SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("单条更新任务缺陷开始......",  user.getLoginId(), user.getUsername(), getClientIp());
		//若 任务缺陷无主键值，说明调用错误
        if (StringUtils.isBlank(taskBug.getTaskBugId())) {
			log.info("单条更新任务缺陷失败，任务缺陷对象taskBugId无属性值",  user.getLoginId(), user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"更新失败，任务缺陷对象taskBugId无属性值"),HttpStatus.OK);
        }
        TaskBug oldTaskBug=taskBugService.get(taskBug.getTaskBugId());
        taskBug.setUpdateTime(new Date().getTime());
        taskBug.setUserId(oldTaskBug.getUserId());
        //taskBug.setState(oldTaskBug.getState());
        //taskBug.setDescript(oldTaskBug.getDescript());
        TaskBug result = taskBugService.save(taskBug);
		log.info("单条更新任务缺陷成功",  user.getLoginId(), user.getUsername(), getClientIp());
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"更新成功",result),HttpStatus.OK);
    }  

    /**
	 *   <p>查询任务缺陷列表，若分页，则页码信息在Request的参数中，包括当前页，每页记录数</p>
     * @return ResponseEntity<QueryResult<taskBug>>，json格式
     * @throws URISyntaxException 如果出错，则产生页码信息的 HTTP headers
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
     */
    @GetMapping("/taskBug/getAll")
	@Operation(summary = "查询，根据传递的条件查询任务缺陷列表，条件参数在Request中") 
	
    public ResponseEntity<QueryResult<TaskBug>> getAll()
        throws URISyntaxException, JsonParseException, JsonMappingException, IOException, ParseException {
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("获取任务缺陷列表开始......",  user.getLoginId(), user.getUsername(), getClientIp());
        QueryFilter<TaskBug> filter=new QueryFilter<TaskBug>(TaskBug.class);
        String infTaskId = request.getParameter("infTaskId");
        if(!StringUtils.isBlank(infTaskId)) {
        	filter.addFilter("Q_infTask.infTaskId_S_EQ", infTaskId);
        }
        filter.addFilterFromRequest(request);
        QueryResult<TaskBug> result= taskBugService.anyQuery(filter);
		log.info("获取任务缺陷列表成功",  user.getLoginId(), user.getUsername(), getClientIp());	
        return new ResponseEntity<QueryResult<TaskBug>>(result,HttpStatus.OK);
    }
    /**
	 *   <p>根据任务缺陷唯一ID值获取任务缺陷</p>
     * @param id 任务缺陷唯一ID值
     * @return  ResponseEntity<taskBug>
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57		
     */
    @GetMapping("/taskBug/get/{id}")
    @Parameter(name = "id", description = "对象唯一Id", required = false)
	@Operation(summary = "获取单个任务缺陷，若根据任务缺陷唯一Id获取到了多个对象则发生异常")
    public ResponseEntity<ResultMsg> get(@PathVariable String id) {
		if(StringUtils.isBlank(id)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取单个任务缺陷失败，未给定任务缺陷的唯一标识"),HttpStatus.OK);
		}SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("获取任务缺陷开始......",  user.getLoginId(), user.getUsername(), getClientIp());
        TaskBug taskBug = taskBugService.get(id);
		/*
		*  <p> ofNullable执行逻辑：如果对象为空，则执行orElse，否则执行.map...
		*		return Optional.ofNullable(sysRole).map(result -> new ResponseEntity<>(result, HttpStatus.OK))
		*		.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));		
		*/
		
		if( null == taskBug ){
			log.info("获取任务缺陷失败",  user.getLoginId(), user.getUsername(), getClientIp());	
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取单个任务缺陷失败，给定的任务缺陷可能被删除"),HttpStatus.OK);
		}
		log.info("获取任务缺陷成功",  user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<ResultMsg>(new ResultMsg(true,"获取成功",taskBug),HttpStatus.OK);
    } 

    /**
     *     <p>根据任务缺陷唯一ID值删除单个任务缺陷</p>
     * @param  id 任务缺陷唯一ID值
     * @return ResponseEntity,json格式
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57		 
     */
    @GetMapping("/taskBug/delete/{id}")
    @Parameter(name = "id", description = "任务缺陷唯一Id", required = false)
	@Operation(summary = "删除单个任务缺陷")
    public ResponseEntity<ResultMsg> delete(@PathVariable String id) {
		if(StringUtils.isBlank(id)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"删除单个任务缺陷失败，未给定任务缺陷的唯一标识"),HttpStatus.OK);
		}SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("删除单条任务缺陷开始......",  user.getLoginId(), user.getUsername(), getClientIp());
		taskBugService.remove(id);
		log.info("删除单条任务缺陷成功",  user.getLoginId(), user.getUsername(), getClientIp());		
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"删除成功"),HttpStatus.OK);
    }
    
    /**
     * 
     * <p> 任务缺陷修改  </p>  
     * @param indexId
     * @param descript
     * @param ids
     * @return
     * @return ResponseEntity<ResultMsg>
     * @throws  
     * @author litao
     * @date 2021年5月19日 
     * @time 上午10:40:47
     */
    @GetMapping("/taskBug/updateState/{indexId}/{descript}/{ids}")
	@Parameters({@Parameter( name = "indexId", description = "任务缺陷唯一Id", required = false),
			@Parameter(name = "descript", description="解决方案", required = false),
			@Parameter(name = "ids", description="判断数字", required = false)
	})
	@Operation(summary = "修改状态")
    public ResponseEntity<ResultMsg> updateState(
    		@PathVariable String indexId
    		,@PathVariable String descript
    		,@PathVariable int ids){
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
    	taskBugService.updateStateById(indexId,ids, user,descript);
    	return new ResponseEntity<ResultMsg>(new ResultMsg(true,"成功"),HttpStatus.OK);
    }
    
    
	/**
	 * 
	 * <p> 解决任务  </p>  
	 * @param id
	 * @return
	 * @return ResponseEntity<ResultMsg>
	 * @throws  
	 * @author litao
	 * @date 2022年3月7日 
	 * @time 下午4:32:30
	 */
	@GetMapping("/taskBug/resolved/{id}")
	@Parameter(name = "id", description = "对象唯一Id", required = false)
	@Operation(summary = "解决任务")
	
	public ResponseEntity<ResultMsg> resolved(@PathVariable String id){
		if (StringUtils.isEmpty(id)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "解决任务失败，没有给指定的Id"), HttpStatus.OK);
		}SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("解决任务开始......",  user.getLoginId(), user.getUsername(),
				getClientIp());
		TaskBug taskBug = taskBugService.get(id);
		if (StringUtils.isEmpty(taskBug)) {
			log.info("获取任务失败",  user.getLoginId(), user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取单个任务失败，给定的任务可能被删除"), HttpStatus.OK);
		}
		taskBug.setState(TaskBugType.resolved);
		taskBugService.save(taskBug);
		return new ResponseEntity<>(new ResultMsg(true, ""), HttpStatus.OK);
	}
	
	/**
	 * 
	 * <p> 确定任务  </p>  
	 * @param id
	 * @return
	 * @return ResponseEntity<ResultMsg>
	 * @throws  
	 * @author litao
	 * @date 2022年3月7日 
	 * @time 下午4:32:22
	 */
	@GetMapping("/taskBug/determined/{id}")
	@Parameter(name = "id", description = "对象唯一Id", required = false)
	@Operation(summary = "确定任务")
	public ResponseEntity<ResultMsg> determined(@PathVariable String id){
		if (StringUtils.isEmpty(id)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "确定任务失败，没有给指定的Id"), HttpStatus.OK);
		}SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("确定任务开始......",  user.getLoginId(), user.getUsername(),
				getClientIp());
		TaskBug taskBug = taskBugService.get(id);
		if (StringUtils.isEmpty(taskBug)) {
			log.info("获取任务失败",  user.getLoginId(), user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取单个任务失败，给定的任务可能被删除"), HttpStatus.OK);
		}
		taskBug.setState(TaskBugType.Determined);
		taskBugService.save(taskBug);
		return new ResponseEntity<>(new ResultMsg(true, ""), HttpStatus.OK);
	}
}
